﻿#region Copyright (c) 2007 by Dan Shechter
////////////////////////////////////////////////////////////////////////////////////////
////
//  IBNet, an Interactive Brokers TWS .NET Client & Server implmentation
//  by Dan Shechter
////////////////////////////////////////////////////////////////////////////////////////
//  License: MPL 1.1/GPL 2.0/LGPL 2.1
//  
//  The contents of this file are subject to the Mozilla Public License Version 
//  1.1 (the "License"); you may not use this file except in compliance with 
//  the License. You may obtain a copy of the License at 
//  http://www.mozilla.org/MPL/
//  
//  Software distributed under the License is distributed on an "AS IS" basis,
//  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
//  for the specific language governing rights and limitations under the
//  License.
//  
//  The Original Code is any part of this file that is not marked as a contribution.
//  
//  The Initial Developer of the Original Code is Dan Shecter.
//  Portions created by the Initial Developer are Copyright (C) 2007
//  the Initial Developer. All Rights Reserved.
//  
//  Contributor(s): None.
//  
//  Alternatively, the contents of this file may be used under the terms of
//  either the GNU General Public License Version 2 or later (the "GPL"), or
//  the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
//  in which case the provisions of the GPL or the LGPL are applicable instead
//  of those above. If you wish to allow use of your version of this file only
//  under the terms of either the GPL or the LGPL, and not to allow others to
//  use your version of this file under the terms of the MPL, indicate your
//  decision by deleting the provisions above and replace them with the notice
//  and other provisions required by the GPL or the LGPL. If you do not delete
//  the provisions above, a recipient may use your version of this file under
//  the terms of any one of the MPL, the GPL or the LGPL.
////////////////////////////////////////////////////////////////////////////////////////
#endregion
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace IBNet
{
    /// <summary>
    /// An implementation of the ITWSEncoding interface that records
    /// the incoming/outgoing traffic from a TWS server to a binary log file
    /// in specialized format. The stream generated by this encoder can be later consumed
    /// by using a TWSPlaybackClient
    /// </summary>
    internal class TWSPlaybackEncoding : TWSEncoding
    {
        private long _lastEmptySize;
        private Stream _recordStream;
        private BinaryWriter _recordWriter;

        /// <summary>
        /// Construct a recording encoder
        /// </summary>
        /// <param name="stream">The original stream to wrap</param>
        /// <param name="recordStream">The stream to be embedded with the playback metadata</param>
        public TWSPlaybackEncoding(Stream stream, Stream recordStream)
            : base(stream)
        {
            if (!recordStream.CanSeek)
                throw new ArgumentException("must be seekable", "recordStream");

            _recordStream = recordStream;

            _stream = 
                new EchoStream(_stream, _recordStream, EchoStream.StreamOwnership.OwnPrimaryStream);

            _recordWriter = new BinaryWriter(_recordStream);
        }
        
        public override TWSServerInfo DecodeServerInfo()
        {
            HandlePlaybackMetaData(IBPlaybackMessage.Receive);
            return base.DecodeServerInfo();
        }
        public override Messages.Client DecodeClientMessage()
        {
            HandlePlaybackMetaData(IBPlaybackMessage.Receive);
            return base.DecodeClientMessage();
        }
        public override void Encode(TWSClientInfo v)
        {
            HandlePlaybackMetaData(IBPlaybackMessage.Send);
            base.Encode(v);
        }
        public override void Encode(TWSClientId id)
        {
            HandlePlaybackMetaData(IBPlaybackMessage.Send);
            base.Encode(id);
        }
        public override void Encode(Messages.Server msg)
        {
            HandlePlaybackMetaData(IBPlaybackMessage.Send);
            base.Encode(msg);
        }

        private void HandlePlaybackMetaData(IBPlaybackMessage msg)
        {
            // Now that we know the previous message size, seek to the size position
            // in the stream and write the position into the stream, and seek back to
            // where we currently are
            if (_lastEmptySize != 0) {
                long position = _recordStream.Position;
                _recordStream.Seek(_lastEmptySize, SeekOrigin.Begin);
                // Write the newly calculated size of the previous message as uint (32-bit)
                _recordWriter.Write((uint)(position - _lastEmptySize - sizeof(uint)));
                _recordStream.Seek(position, SeekOrigin.Begin);
            }
            // Write that there was a request, specify 0 for size for now (next message will adjust this)
            _recordWriter.Write((uint)msg);
            // Write the time stamp
            _recordWriter.Write(DateTime.Now.ToBinary());
            _lastEmptySize = _recordStream.Position;
            // Write the temporary message size as a uint (32-bit) size
            _recordWriter.Write((uint)0);
        }
    }
}
